﻿using System.Collections.Generic;
using System.Linq;
using Blurift.WPFGLControl;
using Crevice2D.Core.Batches;
using Crevice2D.WPFEditorHost;
using Crevice2D.WPFEditorHost.Infrastructure;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Patyk.MapEditor.Geometry;
using Patyk.MapEditor.Models;

namespace Patyk.MapEditor
{
    public class Level : Editor
    {
        // Flags
        public bool CanPut;

        private readonly Vector2 _quadSize = new Vector2(10, 10);
        private Color _quadColor = Color.Red;

        public List<Vertex> Vertices; 
        public InterpolatedCurve InterpolationCurve;

        public Map ActiveMap;

        #region Loading

        public Level()
        {
            Vertices = new List<Vertex>();
            InterpolationCurve = new InterpolatedCurve();
        }
        public Level(Map map)
        {
            Vertices = new List<Vertex>();
            InterpolationCurve = new InterpolatedCurve();
            ActiveMap = map;
        }

        public override void Initialize()
        {
            base.Initialize();

            LoadContent();
        }

        public override void LoadContent()
        {
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        #endregion

        #region Mouse Event Handlers

        public override void MouseLDown(HwndMouseEventArgs e)
        {
            base.MouseLDown(e);

            AddVertex((int)e.Position.X, (int)e.Position.Y);
        }
        public override void MouseLUp(HwndMouseEventArgs e)
        {
            base.MouseLUp(e);
        }
        public override void MouseRDown(HwndMouseEventArgs e)
        {
            base.MouseRDown(e);
        }
        public override void MouseRUp(HwndMouseEventArgs e)
        {
            base.MouseRUp(e);
        }
        public override void MouseMDown(HwndMouseEventArgs e)
        {
            base.MouseMDown(e);
        }
        public override void MouseMUp(HwndMouseEventArgs e)
        {
            base.MouseMUp(e);
        }

        public override void UpdateMouse(HwndMouseEventArgs e)
        {
            base.UpdateMouse(e);

            Vertices.ForEach(p => p.Update());
        }

        #endregion

        private void AddVertex(int x, int y)
        {
            var v = new Vertex(new Vector2(x, y));
            v.OnMouseOver += new Polygon.FiredEvent(MouseOver);
            v.OnMouseOut += new Polygon.FiredEvent(MouseOut);
            v.OnMouseClicked += new Vertex.FiredEvent(MouseClicked);
            Vertices.Add(v);

            if (Vertices.Count > 3)
                InterpolationCurve = new InterpolatedCurve(Vertices.Select(p => p.Position).ToList());
        }

        #region Draw

        /// <summary>
        /// Draw everything in the level from background to foreground.
        /// </summary>
        public override void Draw()
        {
            RenderManager.PrimitiveBatch.Begin(PrimitiveType.TriangleList);

            foreach (var v in Vertices)
            {
                RenderManager.PrimitiveBatch.DrawQuad(v.Position, _quadSize, v.Color);
            }

            RenderManager.PrimitiveBatch.End();

            RenderManager.PrimitiveBatch.Begin(PrimitiveType.LineList);
            {
                if (ActiveMap != null)
                    ActiveMap.Draw();

                RenderManager.PrimitiveBatch.DrawCurve(InterpolationCurve.Curve, InterpolationCurve.Color);
            }
            RenderManager.PrimitiveBatch.End();

            base.Draw();
        }

        /// <summary>
        /// Funkcja event handler najechania myszka z poligonu/wierzcholku.
        /// </summary>
        /// <param name="sender"></param>
        public void MouseOver(object sender)
        {
            if (sender is Vertex)
            {
                (sender as Vertex).Color = Color.Blue;
            }
            CanPut = false;
        }

        /// <summary>
        /// Funkcja event handler zjechania myszka z poligonu/wierzcholku.
        /// </summary>
        /// <param name="sender"></param>
        public void MouseOut(object sender)
        {
            if (sender is Vertex)
            {
                (sender as Vertex).Color = Color.Red;
            }
            CanPut = true;
        }

        /// <summary>
        /// Funkcja event handler dla klikniecia myszka na wierzcholku grafu
        /// </summary>
        /// <param name="sender"></param>
        public void MouseClicked(object sender)
        {
            
        }

        #endregion
    }
}
